package cn.iocoder.yudao.module.happycore.controller.admin.oxmodel;

import cn.iocoder.yudao.module.happycore.controller.admin.oxmodel.vo.OxModelPageReqVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxmodel.vo.OxModelRespVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxmodel.vo.OxModelSaveReqVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.vo.TrainResponse;
import cn.iocoder.yudao.module.happycore.dal.dataobject.oxmodel.OxModelDO;
import cn.iocoder.yudao.module.happycore.service.oxmodel.OxModelService;
import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 模型")
@RestController
@RequestMapping("/happy-ox-core/ox-mode")
@Validated
public class OxModelController {

    @Resource
    private OxModelService oxModelService;

    @PostMapping("/create")
    @Operation(summary = "创建模型")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-mode:create')")
    public CommonResult<Long> createOxMode(@Valid @RequestBody OxModelSaveReqVO createReqVO) {
        return success(oxModelService.createOxMode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模型")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-mode:update')")
    public CommonResult<Boolean> updateOxMode(@Valid @RequestBody OxModelSaveReqVO updateReqVO) {
        oxModelService.updateOxMode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-mode:delete')")
    public CommonResult<Boolean> deleteOxMode(@RequestParam("id") Long id) {
        oxModelService.deleteOxMode(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模型")
                @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-mode:delete')")
    public CommonResult<Boolean> deleteOxModeList(@RequestParam("ids") List<Long> ids) {
        oxModelService.deleteOxModeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-mode:query')")
    public CommonResult<OxModelRespVO> getOxMode(@RequestParam("id") Long id) {
        OxModelDO oxMode = oxModelService.getOxMode(id);
        return success(BeanUtils.toBean(oxMode, OxModelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模型分页")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-mode:query')")
    public CommonResult<PageResult<OxModelRespVO>> getOxModePage(@Valid OxModelPageReqVO pageReqVO) {
        PageResult<OxModelDO> pageResult = oxModelService.getOxModePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OxModelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模型 Excel")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-mode:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOxModeExcel(@Valid OxModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OxModelDO> list = oxModelService.getOxModePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模型.xls", "数据", OxModelRespVO.class,
                        BeanUtils.toBean(list, OxModelRespVO.class));
    }
    @PostMapping("/trainDoneCallBack")
    @PermitAll
    public CommonResult trainDoneCallBack(@RequestBody TrainResponse trainResponse){
        oxModelService.trainDoneCallBack(trainResponse);
        return success(true);
    }

    @PostMapping("/predictDog")
    public CommonResult predictDog(@RequestParam String imagePath, @RequestParam Long modelId) {
        String name =  oxModelService.predictDogBreed(imagePath, modelId);
        return success(name);
    }
}